import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Paths;

public class RemoteNIOCopy {

	public static void main(String[] args) throws IOException {
		if (args.length < 3) {
			System.err.println("Invalid parameters list. Usage: RemoteNIOCopy <source> <remote host> <remote port> ");
			System.exit(1);
		}
		String src = args[0];
		String host = args[1];
		int port = Integer.parseInt(args[2]);

		long before = System.currentTimeMillis();
		SocketAddress addr = new InetSocketAddress(host, port);
		ByteBuffer bb = ByteBuffer.allocate(100000);

		try (SocketChannel channel = SocketChannel.open(addr);
				FileChannel file = FileChannel.open(Paths.get(src))) {
			while (file.read(bb) != -1) {
				bb.flip();
				channel.write(bb);
				bb.clear();
			}
		}
		long after = System.currentTimeMillis();
		System.out.print("Net NIO : ");
		System.out.println(after - before);
	}
}
